<template>
  <div
    class="mask"
    v-if="showCart && total > 0"
    @click="handelCartShowChange"
  ></div>
  <div class="cart">
    <template v-if="showCart && total > 0">
      <div class="header">
        <div class="header__all" @click="setCartItemChecked(shopId)">
          <span
            class="iconfont  icon-checked"
            :class="{ header__check__active: allChecked }"
          ></span>
          全选
        </div>
        <div class="header__clear">
          <span @click="clearCartProducts(shopId)">
            清空购物车
          </span>
        </div>
      </div>
      <div class="product">
        <template v-for="item of productList" :key="item._id">
          <div v-if="item.count > 0" class="product__item">
            <div
              class="product__item__check iconfont icon-checked "
              :class="{ product__item__check__active: item.check }"
              @click="changeCartItemChecked(shopId, item._id)"
            ></div>
            <img :src="item.imgUrl" alt="" class="product__item__img" />
            <div class="product__item__detail">
              <h4 class="product__item__title">{{ item.name }}</h4>
              <p class="product__item__price">
                <span class="product__item__yen">&yen;{{ item.price }}</span>
                <span class="product__item__origin"
                  >&yen;{{ item.oldPrice }}</span
                >
              </p>
            </div>
            <div class="product__number">
              <span
                class="product__number__minus"
                @click="
                  () => {
                    ChangeCartItemInfo(shopId, item._id, item, -1)
                  }
                "
                >-</span
              >
              <span class="product__number__count">{{ item.count || 0 }}</span>
              <span
                class="product__number__plus"
                @click="
                  () => {
                    ChangeCartItemInfo(shopId, item._id, item, 1)
                  }
                "
                >+</span
              >
            </div>
          </div>
        </template>
      </div>
    </template>
    <div class="check">
      <div class="check__icon">
        <img
          src="http://www.dell-lee.com/imgs/vue3/basket.png"
          alt=""
          class="check__icon__img"
          @click="handelCartShowChange"
        />
        <div class="check__icon_tag">{{ total }}</div>
      </div>
      <div class="check__info">
        总计：<span class="check__info__price">&yen; {{ price }}</span>
      </div>
      <div class="check__btn">
        <router-link :to="`/order/${shopId}`" v-show="total > 0">
          去结算</router-link
        >
      </div>
    </div>
  </div>
</template>
<script>
import { computed, ref } from 'vue'
import { useStore } from 'vuex'
import { useRoute } from 'vue-router'
import { useCommonCartEffect } from '../../effects/commonCartEffect'
// 购物车逻辑
const useCartEffect = () => {
  const store = useStore()
  const shopId = useRoute().params.id
  const cartList = store.state.cartList
  const total = computed(() => {
    const productList = cartList[shopId]?.productList
    let count = 0
    if (productList) {
      for (const i in productList) {
        const product = productList[i]
        count += product.count
      }
    }
    return count
  })

  const allChecked = computed(() => {
    const productList = cartList[shopId]?.productList
    let result = true
    for (const key in productList) {
      const product = productList[key]
      if (product.count > 0 && !product.check) {
        result = false
      }
    }
    return result
  })
  const setCartItemChecked = shopId => {
    store.commit('setCartItemChecked', { shopId })
  }
  const { ChangeCartItemInfo, productList, price } = useCommonCartEffect(shopId)
  const changeCartItemChecked = (shopId, productId) => {
    store.commit('changeCartItemChecked', { shopId, productId })
  }
  const clearCartProducts = shopId => {
    store.commit('clearCartProducts', { shopId })
  }
  return {
    total,
    price,
    productList,
    shopId,
    ChangeCartItemInfo,
    changeCartItemChecked,
    clearCartProducts,
    cartList,
    allChecked,
    setCartItemChecked
  }
}
// 展示购物车
const toggleCartEffect = () => {
  const showCart = ref(false)
  const handelCartShowChange = () => {
    showCart.value = !showCart.value
  }
  return { showCart, handelCartShowChange }
}
export default {
  name: 'Cart',
  setup(props) {
    const {
      total,
      price,
      productList,
      shopId,
      ChangeCartItemInfo,
      clearCartProducts,
      changeCartItemChecked,
      allChecked,
      setCartItemChecked
    } = useCartEffect()
    const { showCart, handelCartShowChange } = toggleCartEffect()
    // console.log(productList)
    return {
      total,
      price,
      clearCartProducts,
      changeCartItemChecked,
      productList,
      ChangeCartItemInfo,
      shopId,
      allChecked,
      setCartItemChecked,
      showCart,
      handelCartShowChange
    }
  }
}
</script>
<style lang="scss" scoped>
@import '../../style/common.scss';
@import url('https://at.alicdn.com/t/font_2553865_vag9kujvd2c.css');
.mask {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 2;
  background: rgba(0, 0, 0, 0.5);
}
.cart {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 10;
  background: $bgColor;
}

.header {
  height: 0.52rem;
  background: #fff;
  box-sizing: border-box;
  border-bottom: 0.01rem solid #f1f1f1;
  display: flex;
  align-items: center;
  padding: 0 0.18rem;
  .header__all {
    width: 0.64rem;
    span {
      margin-right: 0.08rem;
      font-size: 0.2rem;
      color: #ccc;
      vertical-align: middle;
    }
    .header__check__active {
      color: #0091ff;
    }
  }
  .header__clear {
    flex: 1;
    text-align: right;
    font-size: 0.14rem;
    color: #333;
  }
}
.product {
  overflow-y: scroll;
  background: $bgColor;
  .product__item {
    position: relative;
    display: flex;
    padding: 0.12rem 0;
    margin: 0 0.16rem;
    border-bottom: 0.01rem solid $content-bgColor;
    align-items: center;
    .product__item__check {
      font-size: 0.2rem;
      margin-right: 0.2rem;
      color: #ccc;
    }
    .product__item__check__active {
      color: #0091ff;
    }
    .product__item__img {
      width: 0.46rem;
      height: 0.46rem;
      margin-right: 0.16rem;
    }
    .product__item__title {
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: $content-fontColor;
      margin-bottom: 0.06rem;
      @include ellipsis;
    }
    .product__item__detail {
      overflow: hidden;
    }
    .product__item__price {
      line-height: 0.2rem;
      font-size: 0.14rem;
      color: #e93b3b;
      display: flex;
      align-items: center;
      .product__item__yen {
        font-size: 0.12rem;
      }
      .product__item__origin {
        color: #999;
        text-decoration: line-through;
        margin-left: 0.06rem;
        font-size: 0.2rem;
        transform: scale(0.5);
      }
    }
  }
  .product__number {
    position: absolute;
    right: 0;
    bottom: 0.12rem;
    display: flex;
    align-items: center;
    .product__number__count {
      margin: 0 0.1rem;
      display: inline-block;
      line-height: 0.24rem;
      text-align: center;
    }
    .product__number__minus,
    .product__number__plus {
      display: inline-block;
      width: 0.2rem;
      height: 0.2rem;
      border-radius: 50%;
      border: 0.01rem solid #666;
      text-align: center;
      line-height: 0.2rem;
      font-size: 0.2rem;
    }
    .product__number__minus {
      color: #666;
    }
    .product__number__plus {
      background: #0091ff;
      color: $bgColor;
    }
  }
}

.check {
  display: flex;
  align-items: center;
  .check__icon {
    width: 0.84rem;
    display: flex;
    position: relative;
    .check__icon__img {
      display: block;
      width: 0.28rem;
      height: 0.26rem;
      margin: 0.12rem auto;
    }
    .check__icon_tag {
      box-sizing: border-box;
      position: absolute;
      top: 0.04rem;
      left: 0.5rem;
      padding: 0 0.05rem;
      background: #e93b3b;
      text-align: center;
      line-height: 0.2rem;
      border-radius: 50%;
      color: $bgColor;
      transform: scale(0.5);
      transform-origin: left center;
    }
  }
  .check__info {
    flex: 1;
    color: #333;
    font-size: 0.12rem;
    .check__info__price {
      font-size: 0.18rem;
      color: #e93b3b;
    }
  }
  .check__btn {
    width: 0.98rem;
    background: #4fb0f9;
    line-height: 0.5rem;
    text-align: center;
    font-size: 0.14rem;

    a {
      display: block;
      width: 100%;
      height: 100%;
      text-decoration: none;
      color: $bgColor;
    }
  }
}
</style>
